﻿$(function () {
    var x = 20;
    var y = -200;
    $("a.toolimg").mouseover(function (e) {
        x = 10;
        y = -200;
        this.myTitle = "";//document.getElementById("span_" + this.id.split('_')[1]).innerText;
        this.title = "";
        var imgTitle = this.myTitle;// ? "<br/>" + this.myTitle : "";
        var toolimg = "<div id='toolimg'><img src='" + this.href + "' class='bigimg'  alt='预览图'/><\/div>"; //创建 div 元素
        $("body").append(toolimg);	//把它追加到文档中
        //alert(document.body.clientWidth + "_" + e.pageX);
        var imgobj = document.getElementById("img_" + this.id.split('_')[1]);
        var imgwidth = imgobj.width;
        var imgheight = imgobj.height;
        //alert(this.href);
        var _width = 10;
        var _height = 10;
        if (imgwidth > imgheight) {     //判断是width和height哪一个大。大的先处理。设置最大值size
            if (imgwidth > 500) {
                // imgobj.style.width = "500";
                _width = 500;
            }

            imgheight = imgheight * 500 / imgwidth;

            if (imgheight > 350) {

                imgheight = 350;
            }
            _height = imgheight;
        } else {

            if (imgheight > 350) {
                //imgobj.style.height = "350";
                _height = 350;
            }
            imgwidth = imgwidth * 350 / imgheight;
            if (imgwidth > 500) {
                imgwidth = 500;
            }
            //imgobj.style.width = imgwidth;
            _width = imgwidth;
        }




        //if (bigimgheight > 350) { bigimgheight = 350;}
        // alert(_width + "_" + _height);

        if (e.pageY < _height) {
            y = -(_height/2);
        }
        else if ((document.body.clientHeight - e.pageY) < _height) {
            y = -((_height) + 20);
        }
        if (e.pageX < _width && (document.body.clientWidth - e.pageX) < _width)
        {
            
            x =  - (_width/2);
            y = - (_height+10);

           
        }
        else if ((document.body.clientWidth - e.pageX) < _width) {
            x = -(_width + 30);
        }
        $("#toolimg")
            .css({
                "top": (e.pageY + y) + "px",
                "left": (e.pageX + x) + "px"
            }).show("fast");	  //设置x坐标和y坐标，并且显示
    }).mouseout(function () {
        this.title = this.myTitle;
        $("#toolimg").remove();	 //移除
    })
});

